<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>保险公司管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" href="../../common/bootstrap/css/bootstrap.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../common/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../common/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../css/customer_list.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../common/module/dtree/dtree.css" />
    <link rel="stylesheet" type="text/css" href="../../common/module/dtree/font/dtreefont.css" />
</head>
<style>
    .detail_content_wrap{
        padding: 10px;
    }
    .dtree-toolbar-fixed {
        position: absolute;
        right: 5px;
        top: 2px;
        font-style: normal;
        transition: all .3s;
        -webkit-transition: all .3s;
    }
    .dtree-toolbar-fixed a i {
        font-size: 14px;
        display: inline-block;
        margin: 0px 1px;
        color: #fe7786;
    }
    .table_content{
        height: 550px;
        overflow: auto;
        padding:10px 0 0 20px
    }
    .detail_all_box {
        margin-top: 20px;
        padding-left: 40px;
    }
    .more_in{
        margin-left: 30px;
    }
</style>
<body>
<div class="detail_content_wrap">
    <div class="layui-row">
        <div class="layui-col-md6">
            <div class="main_content_wrap">
                <div class="layui-card">
                    <div class="layui-card-header index_statistics_head">
                        <div class="layui-tab layui-tab-brief index_title_tab">
                            <button id="companyBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加一级企业
                            </button>
                        </div>
                    </div>
                    <!--右边树-->
                    <div id="toolbarDiv">
                        <ul id="cellTree" class="dtree" data-id="0"></ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md6 detail_all_box">
            <div class="main_content_wrap" id='companyShowForm'>
            <form lay-filter="companyShowForm" class="layui-form model-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">企业名称</label>
                    <div class="layui-input-block">
                        <input name="company_name" type="text" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">详细地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="detail_address" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">联系人</label>
                    <div class="layui-input-block">
                        <input type="text" name="user_name" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">联系电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="common" class="layui-textarea"></textarea>
                    </div>
                </div>
            </form>
            </div>
        </div>
    </div>
</div>
<!-- 表单弹窗 -->
<script type="text/html" id="companyForm">
    <form lay-filter="companyForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item" id="parentInfo">
            <label class="layui-form-label">上级企业</label>
            <div class="layui-input-block">
                <input name="parent_id" id="parent_id" type="hidden"/>
                <input name="parent_name" id="parent_name" placeholder="" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="txt-impt">*</span>企业名称</label>
            <div class="layui-input-block">
                <input name="company_name" placeholder="请输入企业名称" lay-verify="required" type="text" class="layui-input"
                       maxlength="155" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所在地区</label>
            <div class="layui-input-inline selectArea more_in" >
                <select name="province" id="province" lay-filter="province" lay-search>
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="layui-input-inline selectArea">
                <select name="city" id="city" lay-filter="city" lay-search>
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="layui-input-inline selectArea">
                <select name="district" id="district" lay-search>
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">详细地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" placeholder="请输入详细地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系人</label>
            <div class="layui-input-block">
                <input type="text" name="user_name" placeholder="请输入联系人姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone" placeholder="请输入联系人电话" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="common" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-center">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="companyFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script type="text/javascript" src="../../js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../../js/jquery.cookie.js"></script>
<script type="text/javascript" src="../../common/layui/layui2.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/main.js"></script>
<script type="text/javascript" src="../../js/area.js"></script>

<script>
    layui.use(["tree","layer", "form", "table",'admin','dtree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var dtree = layui.dtree;
        var admin = layui.admin;

        var userInfo = JSON.parse(getSessionStorage('uanme'));;
        var page = 1;
        var cityList = {};      //市
        var districtList = {};	//区县

        form.render();

        dtree.render({
            elem: '#cellTree',
            url:access_baseurl + 'Company/getCompanyList',
            toolbarShow:[],
            initLevel: 1,
            toolbar: true, //更多工具栏用法详见工具栏
            toolbarWay:"fixed",
            line:true,
            toolbarExt: [
                {
                    toolbarId: "add",
                    icon: "dtree-icon-add-circle",
                    title: "新增",
                    handler: function (node, $div) {
                        showAddModel(node);//调用添加函数
                    }
                },
                {
                    toolbarId: "edit",
                    icon: "dtree-icon-bianji",
                    title: "编辑",
                    handler: function (node, $div) {
                        showEditModel(node);//调用更新函数
                    }
                },
                {
                    toolbarId: "del",
                    icon: "dtree-icon-roundclose",
                    title: "删除",
                    handler: function (node, $div) {
                        var id = node.nodeId;
                        doDelete(id);//调用删除函数
                    }
                }],
        });
        dtree.on("node('cellTree')", function(obj){
            var nodeId = obj.param.nodeId;
            send_req('Company/getInfo', {id: nodeId},function (res) {
                form.val('companyShowForm', res);
                form.render();
            }, false);
        });

        // 添加按钮点击事件
        $('#companyBtnAdd').click(function () {
            var node = {
                nodeId:0,
                parentId:'-1'
            };
            showAddModel(node);
        });

        // 表单提交
        function showAddModel(node) {
            admin.open({
                type: 1,
                area: '800px',
                offset: '65px',
                title: '添加企业信息',
                content: $('#companyForm').html(),
                success: function (layero, index) {
                    // console.log(layero)
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    generateFirstLevelarea();
                    var id = node.nodeId;
                    if(!id){
                        $('#parentInfo').hide();
                    }else {
                        $('#parentInfo').show();
                    }
                    $('#parent_id').val(id);
                    $('#parent_name').val(node.context);
                    form.render();
                }
            });
        }

        function showEditModel(node) {
            var parentId = node.parentId;
            var id = node.nodeId;
            admin.open({
                type: 1,
                area: '800px',
                offset: '65px',
                title: '修改企业信息',
                content: $('#companyForm').html(),
                success: function (layero, index) {
                    // console.log(layero)
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    generateFirstLevelarea();
                    if(parentId > 0){
                        $('#parentInfo').show();
                    }else {
                        $('#parentInfo').hide();
                    }
                    form.render();
                    send_req('Company/getInfo', {id: id, showCity: 1},function (dataInfo) {
                        if (dataInfo) {
                            var city = dataInfo.city;
                            var district = dataInfo.district;
                            cityList = dataInfo.city_list;
                            districtList = dataInfo.district_list;

                            var cityStr = '<option value="">请选择</option>';
                            for (var i = 0; i < cityList.length; i++) {
                                if (city == cityList[i].name) {
                                    cityStr += '<option selected value="' + cityList[i].name + '">' + cityList[i].name + '</option>';
                                } else {
                                    cityStr += '<option value="' + cityList[i].name + '">' + cityList[i].name + '</option>';
                                }
                            }
                            $('#city').html(cityStr);

                            var districtStr = '<option value="">请选择</option>';
                            for (var i = 0; i < districtList.length; i++) {
                                if (district == districtList[i].name) {
                                    districtStr += '<option selected value="' + districtList[i].name + '">' + districtList[i].name + '</option>';
                                } else {
                                    districtStr += '<option value="' + districtList[i].name + '">' + districtList[i].name + '</option>';
                                }
                            }
                            $('#district').html(districtStr);
                            form.val('companyForm', dataInfo);
                            form.render();
                        }
                    });
                    form.render();
                }
            });
        }

        form.on('submit(companyFormSubmit)', function (d) {
            send_req('Company/modifyCompany', d.field,function (res) {
                layer.closeAll('loading');
                layer.msg('操作成功', {icon: 1}, function () {
                    dtree.reload('cellTree');
                });
                layer.closeAll('page');
            },false);
            return false;
        });
        function doDelete(id) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                send_req('Company/del', {id: id},function (res) {
                    layer.closeAll('loading');
                    layer.msg(res.obj, {icon: 1}, function () {
                        dtree.reload('cellTree');
                    });
                    layer.closeAll('page');
                },false);
                return false;
            });
        }

        /***************地址区域初始化**********************/
        function generateFirstLevelarea() {
            var area_first_str = '<option value="">请选择</option>';
            for (var i = 0; i < provinceList.length; i++) {
                area_first_str += '<option value="' + provinceList[i].value + '">' + provinceList[i].value + '</option>';
            }
            $('#province').html(area_first_str);
        }

        form.on('select(province)',function (data) {
            var selectedFirstLevel = data.value;
            // 清空并隐藏第二、三级菜单
            $('#city').empty().attr('disabled', true);
            $('#district').empty().attr('disabled', true);
            // 生成第二级菜单
            getSencondLevelarea(selectedFirstLevel)
            form.render();
        });

        function getSencondLevelarea(selectedFirstLevel){
            var secondLevelOptions = '';
            if (selectedFirstLevel) {
                var firstLevelData = provinceList.find(item => item.value === selectedFirstLevel);
                if (firstLevelData && firstLevelData.children && firstLevelData.children.length > 0) {
                    secondLevelOptions = '<option value="">请选择</option>';
                    for (var j = 0; j < firstLevelData.children.length; j++) {
                        secondLevelOptions += '<option value="' + firstLevelData.children[j].value + '">' + firstLevelData.children[j].value + '</option>';
                    }
                    $('#city').html(secondLevelOptions).attr('disabled', false);
                    form.render();
                    // 预备生成第三级菜单的逻辑，这里假设第二级选择后会填充第三级
                }
            }
        }
        form.on('select(city)',function (data) {
            var selectedSecondLevel = data.value;
            getThreeLevelarea(selectedSecondLevel)
            form.render();
        });
        function getThreeLevelarea(selectedSecondLevel){
            var selectedFirst = $('#province').val();
            var firstLevelData = provinceList.find(item => item.value === selectedFirst);
            var secondLevelData = firstLevelData.children.find(item => item.value === selectedSecondLevel);
            if (secondLevelData && secondLevelData.children && secondLevelData.children.length > 0) {
                var thirdLevelOptions = '<option value="">请选择</option>';
                for (var k = 0; k < secondLevelData.children.length; k++) {
                    thirdLevelOptions += '<option value="' + secondLevelData.children[k].value + '">' + secondLevelData.children[k].value + '</option>';
                }
                $('#district').html(thirdLevelOptions).attr('disabled', false).parent().show();
            } else {
                $('#district').empty().attr('disabled', true);
            }
            form.render();
        }
        /***************地址区域初始化**********************/

        //缓存当前操作的是哪个表格的哪个tr的哪个td
        $(document).off('mousedown','.layui-table-grid-down').on('mousedown','.layui-table-grid-down',function (event) {
            //直接记录td的jquery对象
            table._tableTrCurrr = $(this).closest('td');
        });

        //给弹出的详情里面的按钮添加监听级联的触发原始table的按钮的点击事件
        $(document).off('click','.layui-table-tips-main [lay-event]').on('click','.layui-table-tips-main [lay-event]',function (event) {
            var elem = $(this);
            var tableTrCurrr =  table._tableTrCurrr;
            if(!tableTrCurrr){
                return;
            }
            var layerIndex = elem.closest('.layui-table-tips').attr('times');
            layer.close(layerIndex);
            table._tableTrCurrr.find('[lay-event="' + elem.attr('lay-event') + '"]').first().click();
        });
    });
</script>

</body>
</html>